
let mongoose = require("mongoose");
// console.log(mongoose);

// 建立链接->选择数据库demo
mongoose.connect("mongodb://127.0.0.1/demo", { useNewUrlParser: true, useUnifiedTopology: true })
// mongoose.disconnect(); //断开链接


// 这种也可以看数据库是否连接成功
mongoose.connection.on("connected", () => {
    console.log("数据库链接成功");
})

mongoose.connection.on("error", (err) => {
    console.log("数据库链接失败", err);
})

mongoose.connection.on("disconnected", () => {
    console.log("数据库断开链接");
})

// 定义表结构(Schema模板 只用来定义数据结构)
// 用户表 users
/* let Schema = mongoose.Schema;   //构造函数 => 用于定义表结构(此时还和链接对应的数据集合建立链接)
let UserSchema = new Schema({
    name: String,  // 单类型显示
    age: {
        type: Number,
        require: true,
        validate: function (val) {  // val 接收的数值  -> 可以自定义验证规则  如果函数返回true就表示验证成功 否则就验证失败
            return val >= 0 && val <= 200 && val % 1 == 0;
        }
    },
    height: Number,
    weight: Number,
    hobby: {
        type: Array,
        required: true,
        default: [],
    }
})


// Model  => 通过UserSchema限制集合中的数据结构
// `Schema`用来也只用来定义数据结构，具体对数据的增删改查操作都由  `Model`  来执行 
// let User = mongoose.model("user",UserSchema);   // 默认会建立新表  users
let User = mongoose.model("user",UserSchema,"users");   // 链接已存在的表(users)

// 查询users表

User.find({},{}).then(list=>{
    console.log("查询成功",list);
}).catch(err=>{
    console.log("err",err);
})
 */





// 成绩表 grades
let Schema = mongoose.Schema;
let gradeSchema = new Schema({
    name: {
        type: String,
        require: true,
    },
    class: {
        type: String,
        enum: ["2301", "2302", "2303", "2304", "2305"],
    },
    chinese: {
        type: Number,
        min: 0,
        max: 100,
    },
    math: {
        type: Number,
        min: 0,
        max: 100,
    },
    english: {
        type: Number,
        min: 0,
        max: 100,
    },
    update_time: {
        type: Date,
        default: new Date().toLocaleString()
    }
})

// 链接mongodb数据库中的集合 (没有就新增)
let Grade = mongoose.model("grade", gradeSchema);



// 增
let gradeList = [
    { "name": "王五", "class": "2302", "chinese": 55, "english": 77, "math": 66 },
    { "name": "小六", "class": "2302", "chinese": 100, "math": 92, "english": 100 },
    { "name": "韦酱", "class": "2302", "chinese": 91, "math": 88, "english": 75 },
    { "name": "卢本伟", "class": "2302", "chinese": 67, "math": 92, "english": 48 },
    { "name": "小甜甜", "class": "2302", "chinese": 95, "math": 47, "english": 73 },
    { "name": "刀小刀", "class": "2302", "chinese": 91, "math": 58, "english": 63 },
    { "name": "陈世豪", "class": "2302", "chinese": 87, "math": 79, "english": 72 },
    { "name": "你好！", "class": "2302", "chinese": 42, "math": 71, "english": 51 },
    { "name": "黎朔", "class": "2302", "chinese": 96, "math": 69, "english": 70 },
    { "name": "小五", "class": "2302", "chinese": 54, "math": 48, "english": 91 },
    { "name": "小桥流水", "class": "2302", "chinese": 63, "math": 78, "english": 40 },
    { "name": "黎梦", "class": "2302", "chinese": 59, "math": 85, "english": 46 },
    { "name": "安吉拉宝贝", "class": "2302", "chinese": 81, "math": 77, "english": 45 },
    { "name": "吴浩", "class": "2302", "chinese": 87, "math": 81, "english": 59 },
    { "name": "方丹", "class": "2302", "chinese": 77, "math": 62, "english": 41 },
    { "name": "简自豪", "class": "2302", "chinese": 94, "math": 75, "english": 42 },
    { "name": "PDD", "class": "2302", "chinese": 44, "math": 46, "english": 100 },
    { "name": "王qi文", "class": "2302", "chinese": 44, "math": 87, "english": 49 },
    { "name": "杨秀丽", "class": "2302", "chinese": 69, "math": 86, "english": 92 },
    { "name": "二狗", "class": "2302", "chinese": 92, "math": 50, "english": 71 }
];
// Grade.insertMany(gradeList).then(list=>{
//     console.log("新增成功",list);
// }).catch(err=>{
//     console.log("新增失败",err);
// })

// let info = { "name": "张三", "class": "2306", "chinese": 92, "math": 50, "english": 71 };
// Grade.insertMany(info).then(list => {
//     console.log("新增成功", list);
// }).catch(err => {
//     console.log("新增失败", err);
// })


// Grade.find()
// Grade.findById()
// Grade.findByIdAndDelete()
// Grade.findByIdAndUpdate()
// Grade.findOneAndDetele()
// Grade.findOneAndUpdate()
// Grade.findOneAndReplace()

// Grade.find().count()  计数
// Grade.find().skip(m).limit(n)  分页  

// Grade.find({},{__v:0}).then(list=>{
//     console.log("查询成功",list.length);
// }).catch(err=>{
//     throw err;
// })

// Grade.find({},{__v:0}).count().then(num=>{
//     console.log("查询成功",num);
// }).catch(err=>{
//     throw err;
// })

// Grade.find({},{__v:0}).skip(0).limit(5).then(list=>{
//     console.log("查询成功",list);
// }).catch(err=>{
//     throw err;
// })

// Grade.findById("64531f8a387536d22e5671eb").then(doc=>{
//     console.log("查询成功",doc);
// }).catch(err=>{
//     throw err;
// })

// Grade.findOne({_id:"64531f8a387536d22e5671eb"}).then(doc=>{
//     console.log("查询成功",doc);
// }).catch(err=>{
//     throw err;
// })



// Grade.deleteOne()
// Grade.deleteMany()

Grade.deleteOne({ name: "王五" }).then(info => {
    console.log("删除成功", info);
}).catch(err => {
    console.log(err);
    // throw err;
})

// Grade.updateOne()
// Grade.updateMany()

// Grade.updateOne({ name: "王五1123" }, { $set: { chinese: 56 } }).then(info => {
//     console.log("更新成功", info);
// }).catch(err => {
//     console.log(err);
//     // throw err;
// })












